************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Figure 4 (Factors Interfering with Sleep; Take-up and Usage of Devices to Improve Sleep)
* Last edited: 07 May 2021
************************************************
**** Panel A ****

/* Daily survey - percent of participants who ever report having each sleep problem, categorized */
use "$d/sleepfactorsaid_dataset.dta", replace
	preserve

	* Drop observations where participant didn't answer section K questions that day
	drop if ds_k1 == .
	
	* Collapse the dataset - each ds_k`i' variable = 1 if factor has ever bothered them; 0 ootherwise
	collapse (max) ds_k1 ds_k2 ds_k3 ds_k4 ds_k5 ds_k6 ds_k7 ds_k8 ds_k9 ds_k10 ds_k11 ds_k12 ds_k13 ds_k14, by(pid)
	
	* Adjust the data so that the bar graph will graph percentages
	replace ds_k1 = 1 if ds_k1 == 2
	replace ds_k2 = 1 if ds_k2 == 2
	replace ds_k3 = 1 if ds_k3 == 2
	replace ds_k4 = 1 if ds_k4 == 2
	replace ds_k5 = 1 if ds_k5 == 2
	replace ds_k6 = 1 if ds_k6 == 2
	replace ds_k7 = 1 if ds_k7 == 2
	replace ds_k8 = 1 if ds_k8 == 2
	replace ds_k9 = 1 if ds_k9 == 2
	replace ds_k10 = 1 if ds_k10 == 2
	replace ds_k11 = 1 if ds_k11 == 2
	replace ds_k12 = 1 if ds_k12 == 2
	replace ds_k13 = 1 if ds_k13 == 2
	replace ds_k14 = 1 if ds_k14 == 2
	
	collapse (mean) ds_k1 ds_k2 ds_k3 ds_k4 ds_k5 ds_k6 ds_k7 ds_k8 ds_k9 ds_k10 ds_k11 ds_k12 ds_k13 ds_k14
	
	foreach var in ds_k1 ds_k2 ds_k3 ds_k4 ds_k5 ds_k6 ds_k7 ds_k8 ds_k9 ds_k10 ds_k11 ds_k12 ds_k13 ds_k14 {
		replace `var' = `var' * 100
	}
	
	* Reshape the data so it can be graphed
	gen temp = 1
	reshape long ds_k, i(temp) j(xloc)
	rename ds_k sleep_factor
	
	* Adjust the x-location of bar so it's easier to separate the groups
	replace xloc = xloc + 0.5 if xloc > 8
	replace xloc = xloc + 0.5 if xloc > 11.5
	
	
	* Generate bar graph 
	#d;
	graph twoway (bar sleep_factor xloc if xloc <= 8,  barw(0.7) color(navy) lwidth(medium))
		(bar sleep_factor xloc if xloc > 8 & xloc <= 11.5,  barw(0.7) color(forest_green) lwidth(medium))
		(bar sleep_factor xloc if xloc > 12,  barw(0.7) color(maroon) lwidth(medium)),
		graphregion(color(white))
		legend(off)
		xtitle("") ytitle("Percent of Participants")
		xlabel(1 "Mosquitoes" 2 "Noise" 3 "Heat" 4 "Cold" 5 "Light" 6 "Bathroom Usage" 7 "People in Room"
		8 "Uncomf. Conditions" 9.5 "Worries or Stress" 10.5 "Insomnia" 11.5 "Bad Dreams"
		13 "Physical Pain" 14 "Disease or Illness" 15 "Hunger or Thirst", labsize(medsmall) angle(38))
		ylabel(0(10)100, labsize(small))
		text(98 4.5 "Environmental Conditions", size(med)) text(98 10.5 "Mental Distress", size(med)) 
		text(98 14 "Physical Distress", size(med))
		scale(0.95) xsize(6) ysize(4)
	;#d cr

		graph export "$oa/Figures/Figure_A4/ds_sleep_factors_categorized.eps", replace

**** Panel C ****		
		
		
			restore
	
	* Generate dummy for each different sleep aid used
	foreach var in k31 k32{
		replace ds_`var' = regexr(ds_`var',"998","") // not interested in sleep aids other than the ones offered
		gen sleepaid_`var'_1 = (strpos(ds_`var',"1") != 0 & strpos(ds_`var',"10") == 0) //  separete taking aid #1 and taking no aid (#10)
		replace sleepaid_`var'_1 = . if mi(ds_`var')
		forval i = 2(1)10{
			gen sleepaid_`var'_`i' = (strpos(ds_`var',"`i'") != 0)
			replace sleepaid_`var'_`i' = . if mi(ds_`var')
		}
	}	

	preserve
		gen id = _n
		reshape long sleepaid_k31_, i(id) j(value)
		
		gen treat_status = (treatment_group != 0)
		
		* Creating order of values
		
		generate order = 1 if value == 1
		replace order = 2 if value== 3
		replace order = 3 if value==4
		replace order = 4 if value == 8
		replace order = 5 if value == 9
		replace order = 6 if value == 5
		replace order = 7 if value == 7
		replace order = 8 if value == 6
		replace order = 9 if value == 2
		replace order = 10 if value==10
		
		#d;
			graph hbar sleepaid_k31,
			over(treat_status, relabel(1 "Control" 2 "Night-Sleep"))
			over(value, relabel(1 "Bedsheet" 2 "Fan" 3 "Pillow" 4 "Blanket" 5 "Cot" 6 "Eyeshades" 7 "Earplugs" 8 "Mattress" 9 "Mat" 10 "None") sort(order) )
			asyvars bar(1, color(gs13)) bar(2, color(navy))
			graphregion(fcolor(white))
			legend(ring(0) pos(4) size(*.75) region(lwidth(none)))
			scale(0.95) xsize(4) ysize(4)
			ytitle("Fraction of Participants-Days",  height(4))
		;
		#d cr
		
		
		
		graph export "$oa/Figures/Figure_A4/sleep_aid_usage.eps", replace
		
	restore

***** Panel B ******	

	  *** Include the information for the number of items participants chose for PID before 5191 from daily sheet
      import excel "$d/sleepaid_beforepid5191.xlsx", sheet("Sheet2") clear
	  
	  rename A pid 
	  
	  foreach var in C D E F G H I J K L M N O P Q R S {
	    
		  replace `var' = 0 if mi(`var')
		
	  }
	  
	  gen num_cot = C
	  egen num_mattress = rowtotal(D E F G H)
	  egen num_blanket = rowtotal(I J K)
	  egen num_pillow = rowtotal(L M N)
	  egen num_eyeshade = rowtotal(O P)
	  gen num_earplug = Q
	  * each time install mattress, always coverd with bed sheet
	  gen num_bedsheet = num_mattress 
	  gen num_fan = S
	  
	  tempfile before
	  save `before', replace
	
	  *** Use main dataset
      use "$d/sleepaid_installation_cleaned.dta", clear
	  
	  drop if pid == 2545
	
      gen cot=0
      replace cot=1 if strmatch(p5, "1 *") | strmatch(p5, "1")
      ren cot aid_5
      
      gen mattress=0
      replace mattress=1 if strmatch(p5, "* 2*") | strmatch(p5, "2*") | ///
	strmatch(p5, "* 3*") | strmatch(p5, "3*") | /// 
	strmatch(p5, "* 4*") | strmatch(p5, "4*") | /// 
	strmatch(p5, "* 5*") | strmatch(p5, "5*") | /// 
	strmatch(p5, "* 6*") | strmatch(p5, "6*") | /// 
	strmatch(p5, "* 16*") | strmatch(p5, "16*")
      gen bedsheet = mattress
      ren bedsheet aid_1
      ren mattress aid_4
      
      gen pillow=0
      replace pillow=1 if strmatch(p5, "* 10*") | strmatch(p5, "10*") | strmatch(p5, "* 11*") | strmatch(p5, "11*") | strmatch(p5, "* 12*") | strmatch(p5, "12*")
      ren pillow aid_2
      
      gen blanket=0
      replace blanket=1 if strmatch(p5, "* 7*") | strmatch(p5, "7*") | strmatch(p5, "* 8*") | strmatch(p5, "8*") | strmatch(p5, "* 9*") | strmatch(p5, "9*")
      ren blanket aid_3
      
      gen eyeshade=0
      replace eyeshade=1 if strmatch(p5, "* 13*") | strmatch(p5, "13*") | strmatch(p5, "* 14*") | strmatch(p5, "14*")
      ren eyeshade aid_7
      
      gen earplugs=0
      replace earplugs=1 if strmatch(p5, "* 15") | strmatch(p5, "15*")
      ren earplugs aid_6
      
      gen fan=0
      replace fan=1 if strmatch(p5, "* 17*") | strmatch(p5, "17*")
      ren fan aid_8
      
      gen sleep_aids_n = aid_1 + aid_2 + aid_3 + aid_4 + aid_5 + aid_6 + aid_7 + aid_8
      gen none = (sleep_aids_n == 0)
      ren none aid_9
	  
	  distinct pid
	  local pids r(ndistinct) 
	  
	preserve
	
	reshape long aid_, i(pid) j(value)
	  	
	graph hbar (mean) aid_,  ///
	over(value, relabel(1 "Bedsheet" 2 "Pillow" 3 "Blanket" 4 "Mattress" 5 "Cot" 6 "Earplugs" 7 "Eyeshades" 8 "Fan" 9 "None")) ///
	ytitle(Fraction of Participants) ///
	graphregion(color(white)) ///
	scale(0.95) xsize(4) ysize(4)
	
	graph export "$oa/Figures/Figure_A4/fraction_sleepaids_install.png", replace
	graph export "$oa/Figures/Figure_A4/fraction_sleepaids_install.eps", replace
	
	restore
